<?php

namespace App\Admin\Forms\User;

use App\Wen\Utils\Settings;
use App\Wen\Utils\UserUtils;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Support\Facades\Cache;

class UserJewelryForm extends Form implements LazyRenderable
{
    use LazyWidget;

    /**
     * 处理表单请求.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
        $user_id = $input['id'];
        $honor = $input['honor'];
        $avatar_frame = $input['avatar_frame'];
        if(_empty_($user_id)){
            return $this->response()->error('参数错误');
        }

        if(!_empty_($honor)){
            update_user_meta($user_id, 'honor', $honor);
        }else{
            update_user_meta($user_id, 'honor', null);
        }

        if(!_empty_($avatar_frame)){
            update_user_meta($user_id, 'cur_avatar_frame', $avatar_frame);
        }else{
            update_user_meta($user_id, 'cur_avatar_frame', null);
        }

        Cache::forget('get:cached:user:'.$user_id);

        return $this->response()
            ->success('操作成功.')
            ->refresh();
    }

    /**
     * 构建表单.
     */
    public function form()
    {
        $user_id = $this->payload["id"];
        $this->hidden('id')->value($user_id);
        $this->image('honor', '头衔')->url('files/uploads')->uniqueName()->autoUpload()->default(get_user_meta($user_id, 'honor'));
        $this->image('avatar_frame', '头像框')->url('files/uploads')->uniqueName()->autoUpload()->default(get_user_meta($user_id, 'cur_avatar_frame'));
    }
}
